<%@page import="org.apache.commons.lang.StringUtils"%>
<%@page import="java.rmi.RemoteException"%>
<%@page import="com.fasterxml.jackson.databind.ObjectMapper"%>
<%@page import="chances.wechat.msggw.resp.BaseResponse"%>
<%@page import="java.io.*"%>
<%@page import="com.squareup.okhttp.*"%>
<%@page import="chances.wechat.msggw.resp.MsgResponse"%>
<%@page import="chances.wechat.msggw.context.BaseContext"%>
<%@page import="java.util.*"%>
<%@page import="chances.wechat.msggw.context.MessageContext"%>
<%@page import="chances.wechat.utils.HttpUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%!
public <T> T parse(String response, Class<T> clazz) throws RemoteException {
	ObjectMapper mapper = new ObjectMapper();
	try {
		return (T) mapper.readValue(response, clazz);
	} catch (Exception e) {
		throw new RemoteException(e.getMessage(), e);
	}

}
%>
<%
//String serverUrl = "http://14.29.2.24:8080/push/mgr.do";
String serverUrl = "http://14.29.2.21:8080/push/mgr.do";
//String stbNo = "088820140113195";  // 机顶盒类型
String stbNo = "0888201502031106";  // 机顶盒类型
String paramStb = request.getParameter("stbNo");
if(StringUtils.isNotEmpty(paramStb)) {
	stbNo = paramStb;
}
//String gatewayAppid = "300"; // msg.gateway.appid
String gatewayAppid = "600"; // msg.gateway.appid
String gatewaySid = "2"; // msg.gateway.sid
String gatewayKey = "xInw6@6eI"; // msg.gateway.key
//String gatewayKey = "123456"; // msg.gateway.key

String body = "";
MessageContext msgContext = new MessageContext();
//msgContext.setContent(url);
/* msgContext.setEpg("defaultsmchd");
msgContext.setReceivers(stbNo);
msgContext.setMsgid("1"); */
msgContext.setCmd("queryUserStatus");

Map<String, String> params = new LinkedHashMap<String, String>();
params.put("cmd", msgContext.getCmd());
params.put("sid", gatewaySid);
params.put("appid", gatewayAppid);
params.put("appacct", stbNo);
params.put("timestamp", msgContext.getTimestamp() + "");
String sig = BaseContext.sig(params, gatewayKey);
params.put("sig", sig);

/* params.put("starttime", msgContext.getStartTime() + "");
params.put("msgtype", msgContext.getMsgtype() + "");
params.put("expire", msgContext.getExpire() + "");
params.put("epg", msgContext.getEpg());
params.put("receivers", msgContext.getReceivers());
params.put("content", ""); */

System.out.println("push mgr params:"+params);

FormEncodingBuilder formBuilder = new FormEncodingBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
	formBuilder.add(entry.getKey(), entry.getValue());
}


//HttpUtils.post(url, body);
RequestBody formBody = formBuilder.build();
Request req = new Request.Builder().url(serverUrl).post(formBody).build();
System.out.println("push mgr request:"+request);

OkHttpClient client = new OkHttpClient();
MsgResponse result =null;
Response res;
String respContent = "";
try {
	res = client.newCall(req).execute();
	System.out.println("push mgr response:"+res);
	if (res.isSuccessful()) {
		respContent = res.body().string();
		System.out.println("push mgr respContent:"+respContent);
		//result = parse(respContent, MsgResponse.class);
		//((BaseResponse) result).setContent(respContent);
	}
} catch (IOException e) {
	throw new RemoteException(e.getMessage(), e);
}

%>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="/we/jquery-1.7.min.js" type="text/javascript" ></script>
<script src="/we/input.js" type="text/javascript" ></script>
</head>
<body>
	<h3><%="push mgr params:"+params %></h3>
	<h3><%="push mgr response:"+res %></h3>
	<h3><%="push mgr respContent:"+respContent%></h3>

</body>
</html>